package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.bigdata.fault.facde.FaultGenerateFacde;
import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.model.daily.Runlog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RunLogETLManager {
	
	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	
	@Autowired
	private FaultGenerateFacde facde;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select name,log_contents,log_type,recorder,recorder_time,id from t_daily_runlog order by recorder_time asc");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select name,log_contents,log_type,recorder,recorder_time,id from t_daily_runlog"
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "' order by recorder_time asc");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		RunlogSave equipmentSave = new RunlogSave(list);
		equipmentSave.saveToBean();
	}
	public class RunlogSave{
		private List<?> list;
 
		public RunlogSave(List<?> list) {
			super();
			this.list = list;
		}
		public void saveToBean(){
			List<Runlog> resuList=new ArrayList<Runlog>();
			for(Object obj : list){
				Object[] objs = (Object[]) obj;
				String convertStr=objs[1].toString().replaceAll("<BR>","\n").replaceAll("</BR>","\n").replaceAll("</br>","\n").replaceAll("<br>","\n");
				Runlog runlog = new Runlog();
				runlog.setName((String)objs[0]);
				runlog.setLog_contents(convertStr);
				runlog.setLog_type((String)objs[2]);
				runlog.setRecorder((String)objs[3]);
				runlog.setTime((Date)objs[4]);
				runlog.setId((String)objs[5]);
				resuList.add(runlog);
				facde.generateFaultByRunlog(runlog.getId(), runlog.getLog_contents(),runlog.getTime());
			}
			//sql 异常
			try {
				dao.persistentBatchUpdate(Runlog.class, resuList);
				log.info("更新调度日志"+resuList.size());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		/*	//解析 异常
			try {
				runLogToFaultManager.analyzeToFaultRecord(resuList);
			} catch (Exception e) {
				e.printStackTrace();
			}*/
		}
	}
	

}
